const app = getApp()
Page({
  data: {
    comments: [],
    id: '',
    hasMore: true,
    page: 1,
    size: 20
  },
  loadMore() {
    let _this = this
    if (!_this.data.hasMore) {
      return
    }
    // 获取短评
    app.douban.findComments('comments', _this.data.id, _this.data.page++, _this.data.size, function (res) {
      // 若获取条数等于默认20条，则还有数据，loading继续
      if (res.data.comments.length && (res.data.comments.length === _this.data.size)) {
        _this.setData({
          comments: _this.data.comments.concat(res.data.comments)
        })
      }
      // 若获取条数小于默认20条，则没有跟多数据，loading结束
      else if (res.data.comments.length && (res.data.comments.length < _this.data.size)) {
        _this.setData({
          comments: _this.data.comments.concat(res.data.comments),
          hasMore: false
        })
      }
      else {
        _this.setData({
          hasMore: false
        })
      }
    })
  },

  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.loadMore()
  },

  onReachBottom: function () {
    this.loadMore()
  }
})